/*
 * Author: James Harpe
 * 
 * Description: This parent class provides ActionBar functionality for basic activities.
 * 
 */
//  Copyright © 2014 Allen County Public Library.

//        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
package us.in.lib.acpl.Mobile_ACPL.parents;

import us.in.lib.acpl.Mobile_ACPL.Mobile_ACPLActivity;
import us.in.lib.acpl.Mobile_ACPL.R;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;

public abstract class BaseActivity extends Activity {

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		boolean loggedIn=this.getSharedPreferences("myPrefs",
				Context.MODE_WORLD_WRITEABLE).getBoolean("loggedIn", false);
		if(!loggedIn){
			menu.findItem(R.id.logout).setVisible(false);
		}
		
		this.getActionBar().setBackgroundDrawable(
				this.getResources().getDrawable(R.drawable.green_color));
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		Mobile_ACPL app = (Mobile_ACPL)this.getApplication();
		switch (item.getItemId()) {
		case R.id.home:
			
			EasyTracker.getTracker().sendEvent("Navigation", "Home Button Pressed", null, null);
			Intent intent = new Intent(this, Mobile_ACPLActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;

		case R.id.logout:
			
			
			EasyTracker.getTracker().sendEvent("Navigation", "User Logged Out", null, null);
			SharedPreferences prefs = getSharedPreferences("myPrefs",
					Context.MODE_WORLD_WRITEABLE);
			SharedPreferences.Editor editor = prefs.edit();

			editor.putString("user", null);
			editor.putString("pin", null);
			editor.putString("sessionToken", null);
			editor.putBoolean("loggedIn", false);
			editor.commit();

			Toast.makeText(this.getApplicationContext(),
					"You have been logged out.", Toast.LENGTH_SHORT).show();

			Intent intent2 = new Intent(this, Mobile_ACPLActivity.class);
			intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent2);
			return false;

		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
